package com.android.tea.widght;

import static android.view.ViewGroup.LayoutParams.FILL_PARENT;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.tea.R;

public class TEAAlertDialog extends Dialog {

	private static final String TAG = "TEAAlertDialog";

	protected Context mContext;

	// private AlertDialog mAlertDialog;
	private String progressTitle = null;
	private String progressMessage = null;
	private Dialog mDialog;

	private Drawable mIcon;
	private CharSequence mTitle;
	private CharSequence mMessage;

	private ImageView mIconImageView;
	private TextView mAlertDialogTextView;
	private TextView mMessageTextView;

	private LinearLayout contentPanelLayout;
	private LinearLayout topPanelLayout;
	private LinearLayout buttonPanelLayout;
	private LinearLayout twoButtonLayout;
	private Button mButton1;
	private Button mButton2;
	private Button mButton3;

	private Handler mHandler;
	private Message positiveButtonMessage;
	private Message negativeButtonMessage;

	private Button mButtonPositive;
	private Button mButtonNegative;

	private View addView;
	private FrameLayout custom;
	private FrameLayout customPanel;

	private boolean hasButton = false;
	private boolean mIsSingleChoice = false;
	View.OnClickListener mButtonHandler = new View.OnClickListener() {
		public void onClick(View v) {
			Message m = null;
			if (v == mButtonPositive && positiveButtonMessage != null) {
				m = Message.obtain(positiveButtonMessage);
			} else if (v == mButtonNegative && negativeButtonMessage != null) {
				m = Message.obtain(negativeButtonMessage);
			}
			if (m != null) {
				m.sendToTarget();
			}
			mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialog)
					.sendToTarget();
		}
	};

	private List<ButtonBean> buttonData;

	protected TEAAlertDialog(Context context) {
		super(context, R.style.LaBiAlertDialogTheme_white);
		mContext = context;
		// mAlertDialog=new AlertDialog.Builder(context).create();
		this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.labi_alert_dialog2);
		mHandler = new ButtonHandler(this);
		mDialog = this;
		// mView=LayoutInflater.from(context).inflate(R.layout.labi_alert_dialog2,
		// null);

		// mAlertDialog.setView(mView,0,0,0,0);

		// mIconImageView = (ImageView) findViewById(R.id.icon);
		mAlertDialogTextView = (TextView) findViewById(R.id.alertTitle);
		mMessageTextView = (TextView) findViewById(R.id.message);

		contentPanelLayout = (LinearLayout) findViewById(R.id.contentPanel);

		topPanelLayout = (LinearLayout) findViewById(R.id.topPanel);

		buttonData = new ArrayList<ButtonBean>();

		buttonPanelLayout = (LinearLayout) findViewById(R.id.buttonPanel);
		// buttonLayout=(LinearLayout) findViewById(R.id.leftSpacer);
		twoButtonLayout = (LinearLayout) findViewById(R.id.twoButton);
		mButton1 = (Button) findViewById(R.id.button1);
		mButton2 = (Button) findViewById(R.id.button2);
		mButton3 = (Button) findViewById(R.id.button3);

		customPanel = (FrameLayout) findViewById(R.id.customPanel);
		custom = (FrameLayout) findViewById(R.id.custom);
	}

	protected TEAAlertDialog(Context context, int theme) {
		super(context, theme);
		mContext = context;
		// mAlertDialog=new AlertDialog.Builder(context).create();
		this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.labi_alert_dialog2);
		mHandler = new ButtonHandler(this);
		mDialog = this;
		// mView=LayoutInflater.from(context).inflate(R.layout.labi_alert_dialog2,
		// null);

		// mAlertDialog.setView(mView,0,0,0,0);

		// mIconImageView = (ImageView) findViewById(R.id.icon);
		mAlertDialogTextView = (TextView) findViewById(R.id.alertTitle);
		mMessageTextView = (TextView) findViewById(R.id.message);

		contentPanelLayout = (LinearLayout) findViewById(R.id.contentPanel);

		topPanelLayout = (LinearLayout) findViewById(R.id.topPanel);

		buttonData = new ArrayList<ButtonBean>();

		buttonPanelLayout = (LinearLayout) findViewById(R.id.buttonPanel);
		// buttonLayout=(LinearLayout) findViewById(R.id.leftSpacer);
		twoButtonLayout = (LinearLayout) findViewById(R.id.twoButton);
		mButton1 = (Button) findViewById(R.id.button1);
		mButton2 = (Button) findViewById(R.id.button2);
		mButton3 = (Button) findViewById(R.id.button3);

		customPanel = (FrameLayout) findViewById(R.id.customPanel);
		custom = (FrameLayout) findViewById(R.id.custom);
	}

	public void setIcon(int resId) {
		// TODO Auto-generated method stub
		mIcon = mContext.getResources().getDrawable(resId);
		mIconImageView.setBackgroundDrawable(mIcon);
	}

	@Override
	public void setTitle(CharSequence title) {
		// TODO Auto-generated method stub
		mTitle = title;
		mAlertDialogTextView.setText(mTitle);

	}

	public void setMessage(CharSequence message) {
		// TODO Auto-generated method stub
		mMessage = message;
		mMessageTextView.setText(message);
	}

	public void setView(View view) {
		// TODO Auto-generated method stub
		addView = view;
		if (view != null) {
			custom.addView(view, new LayoutParams(FILL_PARENT, FILL_PARENT));
		}
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		if (mTitle == null) {
			topPanelLayout.setVisibility(View.GONE);

		}
		if (mMessage != null) {
			mMessageTextView.setVisibility(View.VISIBLE);
		} else {
			contentPanelLayout.setVisibility(View.GONE);
		}
		if (addView == null) {
			customPanel.setVisibility(View.GONE);
		}
		setupButtons();
		// Window mWindow = mAlertDialog.getWindow();

		// Log.i(TAG, ""+DialogId.getDialogId("topPanel"));
		// LinearLayout topPanel=(LinearLayout) mWindow.findViewById(16908633);
		// if (topPanel != null) {
		// topPanel.setVisibility(View.GONE);
		// }
		// WindowManager.LayoutParams lp = mWindow.getAttributes();
		// // lp.y=SdCardUtil.changeDip2Pixel(mContext, 100);
		// lp.y=-120;
		// lp.x=0;
		// mWindow.setAttributes(lp);
		// mAlertDialog.onWindowAttributesChanged(lp);
		// mAlertDialog.setTitle("郁闷");
		// mWindow.requestFeature(Window.FEATURE_NO_TITLE);
		// mAlertDialog.show();
		try {
			super.show();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void setupButtons() {
		// TODO Auto-generated method stub
		if (buttonData.size() == 0) {
			buttonPanelLayout.setVisibility(View.GONE);
		} else {
			hasButton = true;
			if (buttonData.size() == 1) {// 只有一个按钮的时候
				twoButtonLayout.setVisibility(View.INVISIBLE);
				// mButton1.setVisibility(View.INVISIBLE);
				// mButton2.setVisibility(View.INVISIBLE);
				mButton3.setVisibility(View.VISIBLE);
				mButtonPositive = mButton3;
				mButton3.setText(buttonData.get(0).getButtonName());
				positiveButtonMessage = mHandler.obtainMessage(
						DialogInterface.BUTTON_POSITIVE, buttonData.get(0)
								.getListener());
				mButtonPositive.setOnClickListener(mButtonHandler);
			} else if (buttonData.size() == 2) {
				// buttonLayout.removeView(mButton3);
				twoButtonLayout.setVisibility(View.VISIBLE);
				mButton3.setVisibility(View.INVISIBLE);
				mButton1.setText(buttonData.get(0).getButtonName());
				mButtonPositive = mButton1;
				positiveButtonMessage = mHandler.obtainMessage(
						DialogInterface.BUTTON_POSITIVE, buttonData.get(0)
								.getListener());
				mButtonPositive.setOnClickListener(mButtonHandler);

				mButton2.setText(buttonData.get(1).getButtonName());
				mButtonNegative = mButton2;
				negativeButtonMessage = mHandler.obtainMessage(
						DialogInterface.BUTTON_NEGATIVE, buttonData.get(1)
								.getListener());
				mButtonNegative.setOnClickListener(mButtonHandler);
			}
		}
	}

	public void setPositiveButton(String buttonName,
			DialogInterface.OnClickListener listener) {
		if (TextUtils.isEmpty(buttonName)) {
			return;
		}
		if (listener == null) {
			listener = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub

				}
			};
		}
		ButtonBean buttonBean = new ButtonBean(buttonName, listener);

		int size = buttonData.size();
		while (size > 2) {
			buttonData.remove(0);
			size = buttonData.size();
		}
		buttonData.add(buttonBean);
	}

	public void setNegativeButton(String buttonName,
			final DialogInterface.OnClickListener listener) {
		setPositiveButton(buttonName, listener);
	}

	private void createListView(CharSequence[] items, int checkedItem,
			final OnClickListener listener) {
		ListView listView = new ListView(mContext);
		int layout = R.layout.labi_select_dialog_singlechoice;
		if (items != null) {
			ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
					mContext, layout, R.id.text1, items);
			listView.setAdapter(adapter);
			listView.setDivider(new ColorDrawable(Color.parseColor("#d1d1d1")));
			// listView.setDividerHeight(SdCardUtil.changeDip2Pixel(mContext,
			// 1));
			if (mIsSingleChoice) {
				listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
				if (checkedItem > -1 && checkedItem < items.length) {
					listView.setItemChecked(checkedItem, true);
					listView.setSelection(checkedItem);
				}
			}
		}
		listView.setBackgroundColor(Color.WHITE);
		listView.setCacheColorHint(Color.WHITE);
		if (listener != null) {
			listView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView parent, View v,
						int position, long id) {
					listener.onClick(mDialog, position);
					if (!hasButton) {
						mDialog.dismiss();
					}
				}
			});
		}
		custom.setPadding(0, 0, 0, 0);
		setView(listView);
	}

	public void setSingleChoiceItems(CharSequence[] items, int checkedItem,
			final OnClickListener listener) {
		// TODO Auto-generated method stub
		mIsSingleChoice = true;
		createListView(items, checkedItem, listener);
	}

	public void setItems(CharSequence[] items, final OnClickListener listener) {

		createListView(items, -1, listener);
	}

	@Override
	public void cancel() {
		// TODO Auto-generated method stub
		super.cancel();
	}

	@Override
	public void dismiss() {
		// TODO Auto-generated method stub
		super.dismiss();
	}

	private static final class ButtonHandler extends Handler {
		// Button clicks have Message.what as the BUTTON{1,2,3} constant
		private static final int MSG_DISMISS_DIALOG = 1;

		private WeakReference<DialogInterface> mDialog;

		public ButtonHandler(DialogInterface dialog) {
			mDialog = new WeakReference<DialogInterface>(dialog);
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case DialogInterface.BUTTON_POSITIVE:
			case DialogInterface.BUTTON_NEGATIVE:
			case DialogInterface.BUTTON_NEUTRAL:
				((DialogInterface.OnClickListener) msg.obj).onClick(mDialog
						.get(), msg.what);
				break;

			case MSG_DISMISS_DIALOG:
				((DialogInterface) msg.obj).dismiss();
			}
		}
	}

	@Override
	public void setTitle(int titleId) {
		// TODO Auto-generated method stub
		mTitle = mContext.getResources().getString(titleId);
		mAlertDialogTextView.setText(mTitle);
	}

	public void setMessage(int messageId) {
		mMessage = mContext.getResources().getString(messageId);
		mMessageTextView.setText(mMessage);
	}

	public void setPositiveButton(int buttonNameId,
			DialogInterface.OnClickListener listener) {
		String buttonName = mContext.getResources().getString(buttonNameId);
		this.setPositiveButton(buttonName, listener);
	}

	public void setNegativeButton(int buttonNameId,
			final DialogInterface.OnClickListener listener) {
		this.setPositiveButton(buttonNameId, listener);
	}

	public static class Builder {
		private TEAAlertDialog dialog;

		public Builder(Context context) {
			dialog = new TEAAlertDialog(context);
		}

		public Builder(Context context, int theme) {
			dialog = new TEAAlertDialog(context, theme);
		}

		public Builder setTitle(CharSequence title) {
			dialog.setTitle(title);
			return this;
		}

		public Builder setTitle(int titleId) {
			dialog.setTitle(titleId);
			return this;
		}

		public Builder setMessage(CharSequence message) {
			dialog.setMessage(message);
			return this;
		}

		public Builder setMessage(int messageId) {
			dialog.setMessage(messageId);
			return this;
		}

		public Builder setPositiveButton(int buttonNameId,
				final DialogInterface.OnClickListener listener) {
			dialog.setPositiveButton(buttonNameId, listener);
			return this;
		}

		public Builder setPositiveButton(String buttonName,
				final DialogInterface.OnClickListener listener) {
			dialog.setPositiveButton(buttonName, listener);
			return this;
		}

		public Builder setNegativeButton(String buttonName,
				final DialogInterface.OnClickListener listener) {
			dialog.setNegativeButton(buttonName, listener);
			return this;
		}

		public Builder setNegativeButton(int buttonNameId,
				final DialogInterface.OnClickListener listener) {
			dialog.setNegativeButton(buttonNameId, listener);
			return this;
		}

		public Builder setView(View view) {
			dialog.setView(view);
			return this;
		}

		public TEAAlertDialog create() {
			return dialog;
		}

		public TEAAlertDialog show() {
			dialog.show();
			return dialog;
		}

		public Builder setSingleChoiceItems(CharSequence[] items,
				int checkedItem, final OnClickListener listener) {
			dialog.setSingleChoiceItems(items, checkedItem, listener);
			return this;
		}

		public Builder setItems(CharSequence[] items,
				final OnClickListener listener) {
			dialog.setItems(items, listener);
			return this;
		}

	}

	public static Dialog buildDialog(Context context, final String title,
			String message, String button1, String button2,
			DialogInterface.OnClickListener listernOK) {

		TEAAlertDialog.Builder builder = new TEAAlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		if (button1 != null) {
			builder.setNegativeButton(button1, listernOK);
		}
		if (button2 != null) {
			builder.setPositiveButton(button2, listernOK);
		}
		return builder.create();
	}

}
